<?php
/**
 *
 * User: wufeng
 * Date: 2020/03/07
 */

namespace app\models;
use think\Model;

class MyApprovalModel extends Model
{
	protected $table = 'edu_my_approval';

	/**
	 * User: wufeng
	 * Date: 2020/03/24
	 *
	 * @param $params
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function approvalLists($params)
	{
		$page = isset($params['page']) ? (int) $params['page'] : 1;
		$listRows = isset($params['limit']) ? (int) $params['limit'] : 10;
		$where = [];
		$type = 'no_approval';
		if (isset($params['type']) && '' !== $params['type']) {
			$type = $params['type'];
		}
		if (isset($params['stype']) && '' !== $params['stype']) {
			$where['type'] = $params['stype'];
		}
		if (isset($params['day']) && '' !== $params['day']) {
			$date = explode(' - ', $params['day']);
			$start = date('Y-m-d H:i:s', strtotime($date[0]));
			$end = date('Y-m-d H:i:s', strtotime($date[1]) + 86400);
			if(1 === (int) $params['operation_type']){
				$where['approval_time'] = [['egt', $start], ['lt', $end]];
			}elseif (2 === (int) $params['operation_type']){
				$where['coordination_time'] = [['egt', $start], ['lt', $end]];
			}
		}



		$ids = (new MyApprovalRelationModel())->where(['approver_id' => get_current_id()])->column('approval_id');
		$where['id'] = ['in', implode(',', $ids)];
		switch ($type) {
			case 'no_approval':
				//待审批
				$where['status'] = ['in','1,4'];
				break;
			case 'approved':
				//已审批
				$where['status'] = ['in','2,3'];
		}
		$where['operation_type'] = $params['operation_type'];
		$listData = self::where($where)->order('approval_time desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$item['user_name'] = (new UserModel())->get($item['user_id'])['name'];
				$approver = explode(',',$item['approver']);
				$approver_name = [];
				foreach ($approver as &$value){
					$approver_name[] = (new UserModel())->get($value)['name'];
				}
				$item['approver_name'] = implode(',',$approver_name);
				if($item['user_id'] == get_current_id()){
					unset($item['type']);
					$item['type'] = 3;
					$item['type_text'] = '自己';
				}else{
					$item['type'] = $item['type'];
					if(1 === (int) $item['type']){
						$item['type_text'] = '同事';
					}elseif (2 === (int) $item['type']){
						$item['type_text'] = '学员';
					}
				}
				if(1 === (int) $params['operation_type']){
					if(1 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#c4f2f9;font-weight: bold;">待审批</span>';
					}elseif (2 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#00B83F;font-weight: bold;">已审批</span>';
					}elseif (3 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#c54736;font-weight: bold;">已驳回</span>';
					}elseif (4 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#c09853;font-weight: bold;">已撤销</span>';
					}
				}elseif (2 === (int) $params['operation_type']){
					if(1 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#c4f2f9;font-weight: bold;">待处理</span>';
					}elseif (2 === (int) $item['status']){
						$item['status_text'] = '<span style="color:#00B83F;font-weight: bold;">已处理</span>';
					}
				}

			}
		}

		return $listData;
	}
}